{% extends 'index.html' %}




{% block content %}
    <div class="col-md-12">
        <div class="section-content">
            <div class="content-head"><h4 class="content-title">上传训练文件</h4>
            </div>
            <div class="content-details show">
                <div class="card basic-form">
                    <div class="card-body card-block">

                    <form action="{% url 'train:upload' %}" method="post" id="upload_form" enctype="multipart/form-data" class="form-horizontal">
                           {% csrf_token %}
                             <div class="row form-group">
                                <div class="col col-md-3">
                                    <label for="file-input" class=" form-control-label">
                                        请上传csv文件
                                    </label>
                                </div>
                                <div class="col-12 col-md-9">
                                    <input type="file" id="file-input" name="csv_file" class="form-control-file" accept=".csv"></div>
                            </div>

                            <div class="card-footer">
                            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-dot-circle-o"></i>
                                提交
                            </button>
                            </div>
                    </form>
                    </div>

                </div>
            </div>
        </div>
    </div>
{% endblock %}


{% block script %}
<script src="/static/js/jquery-3.7.0.js"></script>
<script src="/static/js/jquery.validate.min.js"></script>
<script>
$(document).ready(function () {
    $('#upload_form').submit(function (e) {
        e.preventDefault();

        // Create a new FormData object
        let formData = new FormData(this);

        $.ajax({
            url: "{% url 'train:upload' %}",
            method: 'post',
            headers: { 'X-CSRFToken': $('[name="csrfmiddlewaretoken"]').val() },
            data: formData,
            contentType: false,
            processData: false,
            success: function (response) {
                if (response.code === 1) {
                    alert('上传成功! 开始训练！');
                    window.location.href = "{% url 'train:execute_train' %}";
                } else {
                    alert('上传失败');
                    window.location.reload();
                }
            }
        });
    });
});
</script>
{% endblock %}
